/*
 ============================================================================
 Name		: CDownloadEngine.h
 Author	  : William
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CCDownloadEngine declaration
 ============================================================================
 */

#ifndef CDOWNLOADENGINE_H
#define CDOWNLOADENGINE_H

// INCLUDES
#include <e32std.h>
#include <e32base.h>
#include <http\mhttpdatasupplier.h>
#include <http\mhttptransactioncallback.h>
#include <http\mhttpauthenticationcallback.h>
// CLASS DECLARATION


class CDownloadEngine  : public CBase, public MHTTPTransactionCallback
{
public:
	static CDownloadEngine* NewL();
	static CDownloadEngine* NewLC();
	~CDownloadEngine();
	void GetRequestL(const TDesC8& aUri);

    TInt GetHTTPTotalLen();// 这个连个暂时还不需要
    TInt GetHTTPRevcLen();//
    void CreateDownFileName (const TDesC & aFileName,const TDesC & FileNameTmp);
    void StopDowbLoad();

    

private: // from MHTTPTransactionCallback
	virtual void MHFRunL(RHTTPTransaction aTransaction, const THTTPEvent& aEvent);
	virtual TInt MHFRunError(TInt aError, RHTTPTransaction aTransaction, const THTTPEvent& aEvent);

private:
	CDownloadEngine();
	void ConstructL();

	void AddHeaderL(RHTTPHeaders aHeaders, TInt aHeaderField, const TDesC8& aHeaderValue);
	void AddHeaderL(RHTTPHeaders aHeaders, const TDesC8& aHeaderField, const TDesC8& aHeaderValue);
	TBool RecvNotify( TDesC8& aMessage);
	void GetHeaderL(RHTTPHeaders aHeaders, const TDesC8 &aHeaderField, THTTPHdrVal &aHeaderValue ) const;
	void Cancel();
	void SetupConnectionL();
private:
	RHTTPSession  iSession;
	RHTTPTransaction  iTransaction;
    RSocketServ iSocketServ;
    RConnection iConnection;

	HBufC* iResponseBuffer;
	HBufC8* iUri;
	TUriParser8 iUriParser;

	TBool m_is_first_resp ; 

	TInt  m_total_bytes ; 
	TInt  m_recv_bytes;

	RFs   iFs ; 
	RFile iFile ; 
	TBool iConnectionSetupDone;

	TBuf <100> iFileName;
};

/*
SymBian 获取HTTP 消息头中的下载文件长度 
2008-06-09 10:19:21 
在使用HTTP下载文件的时候，需要获取下载文件的大小，下面的方法就是用来获取这个文件大小的方法

获取HTTP 头中响应请求文件的大小: 

RHTTPHeaders headers = resp.GetHeaderCollection ( ); 
THTTPHdrVal aHeaderValue; 

TRAPD(error,GetHeaderL (headers, _L8("Content-Length"), aHeaderValue )) 

void GetHeaderL(RHTTPHeaders aHeaders, 
const TDesC8 &aHeaderField, THTTPHdrVal &aHeaderValue ) 
{ 
RStringPool string_pool = iSession.StringPool ( ); 
RStringF contLength = string_pool.OpenFStringL (aHeaderField ); 

aHeaders.GetField (contLength, 0, aHeaderValue ); 

contLength.Close ( ); 
} 
//可用获取到请求文件的大小,比如下载文件时可用拿到文件的大小.

然后使用 aHeaderValue.int() 来返回 Integer的值

 */
#endif // CDOWNLOADENGINE_H
